package com.foundryx.shared.core.utils;

import cn.hutool.core.lang.Snowflake;

/**
 * @className: IdUtils
 * @author: ocxz1
 * @date: 2025/6/22 上午9:34
 * @Version: 1.0
 * @description: fx-share-resources
 */
public class IdUtils {

    // 私有静态内部类，封装 Hutool 实现
    private static class Internal {
        private static final long WORKER_ID = 1L;
        private static final long DATA_CENTER_ID = 1L;
        private static final Object lock = new Object();
        private static Snowflake snowflake;

        static {
            snowflake = cn.hutool.core.util.IdUtil.getSnowflake(WORKER_ID, DATA_CENTER_ID);
        }
    }

    /**
     * 动态设置 workerId 和 dataCenterId
     */
    public static void setWorkerAndDataCenterId(long workerId, long dataCenterId) {
        synchronized (Internal.lock) {
            Internal.snowflake = cn.hutool.core.util.IdUtil.getSnowflake(workerId, dataCenterId);
        }
    }

    /**
     * 获取雪花ID
     */
    public static long nextId() {
        return Internal.snowflake.nextId();
    }

    /**
     * 获取字符串格式的雪花ID
     */
    public static String nextIdStr() {
        return Internal.snowflake.nextIdStr();
    }

    /**
     * 获取 UUID
     */
    public static String randomUUID() {
        return cn.hutool.core.util.IdUtil.randomUUID();
    }
}
